<?php
/**
 * Created by singwa
 * User: singwa
 * motto: 现在的努力是为了小时候吹过的牛逼！
 * Time: 17:51
 */
declare(strict_types=1);

namespace app\api\controller;

use app\BaseController;

class Login extends BaseController
{
    public function index(): object
    {
     //用户点击发送验证码 把 sms.手机号 存入 redis   内容有 验证码  存活60s

        if (!$this->request->isPost())
            return show(config("status.error"), "非法请求");
        $phoneNumber = input("phone_number",'','trim');
        $code = input("code", 0, "intval");
        $type = input("type", 0, "intval");
        // 参数校验
        $data = [
            'phone_number' => $phoneNumber,
            'code' => $code,
            'type' => $type,
        ];
        $validate = new \app\api\validate\User();
        if (!$validate->scene('login')->check($data))
            return show(config('status.error'), $validate->getError());

    //查看database中有无 该用户名 等等 进行判断  最后加入登录几率中
        try {
            $result = (new \app\common\business\User())->login($data);
        } catch (\Exception $e) {
            return show($e->getCode(), $e->getMessage());
        }

        if ($result) return show(config('status.success'), "登录成功", $result);
        return show(config('status.error'), "登录失败");
    }
}